package com.bupt.liutong.front.bbs.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bupt.liutong.admin.bbs.bo.BoardBo;
import com.bupt.liutong.admin.bbs.bo.PostBo;
import com.bupt.liutong.admin.bbs.form.BoardForm;
import com.bupt.liutong.admin.bbs.form.PostForm;
import com.bupt.liutong.admin.core.base.BaseAction;
import com.bupt.liutong.admin.core.bo.UserBo;
import com.bupt.liutong.admin.core.form.UserForm;
import com.bupt.liutong.util.BeanUtils;
import com.bupt.liutong.util.StringUtils;

public class PostAction extends BaseAction {

	private PostBo postBo;
	private BoardBo boardBo;
	private UserBo userBo;
	
	public ActionForward des(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		// 回复列表
		PostForm postForm = (PostForm) form;
		postForm.setPageSize(8);
		postForm.setParentId(postForm.getId());
		postForm.setId(null);
		List<PostForm> postFormList = postBo.getRepliesByPage(postForm, request);
		//postForm.setId(postForm.getParentId());
		//postForm.setParentId(null);

		// 楼主的帖子
		PostForm lzPostForm = new PostForm();
		BeanUtils.copyProperties(lzPostForm, postForm);
		lzPostForm.setId(postForm.getParentId());
		lzPostForm.setParentId(null);
		lzPostForm = postBo.getPostById(lzPostForm);
		request.setAttribute("lzPostForm", lzPostForm);
		if (postForm.getCurPage() == 1)
			postFormList.add(0, lzPostForm);
		request.setAttribute("postFormList", postFormList);
		putPageInfo(request, postForm);
		
		// 版块名
		BoardForm boardForm = new BoardForm();
		boardForm.setId(lzPostForm.getBoardId());
		boardForm = boardBo.getBoardById(boardForm);
		request.setAttribute("boardName", boardForm.getName());
		
		return mapping.findForward("des");
	}
	
	public ActionForward insertLzPost(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		PostForm postForm = (PostForm) form;
		UserForm userForm = (UserForm) request.getSession().getAttribute("userForm");
		if (userForm == null || StringUtils.isEmpty(userForm.getUserName())) {
			if (!userBo.validateKaptcha(request)) {
				return mapping.findForward("kaptchaError");
			} else {
				postForm.setUserName("游客");
				postForm.setNickName("游客");
				postForm.setLastUserName("游客");
				postForm.setLastNickName("游客");
			}
		} else {
			postForm.setUserName(userForm.getUserName());
			postForm.setNickName(userForm.getNickName());
			postForm.setLastUserName(userForm.getUserName());
			postForm.setLastNickName(userForm.getNickName());
		}
		postForm.setIp(request.getRemoteAddr());
		postBo.insertLzPost(postForm);
		String path = mapping.findForward("insertLzPostSuccess").getPath()
				+ "&id=" + postForm.getBoardId() + "&parentId="
				+ postForm.getBoardParentId();
		return new ActionForward(path, true);
	}
	
	public ActionForward insertReply(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		PostForm postForm = (PostForm) form;
		UserForm userForm = (UserForm) request.getSession().getAttribute("userForm");
		if (userForm == null || StringUtils.isEmpty(userForm.getUserName())) {
			if (!userBo.validateKaptcha(request)) {
				return mapping.findForward("kaptchaError");
			} else {
				postForm.setUserName("游客");
				postForm.setNickName("游客");
				postForm.setLastUserName("游客");
				postForm.setLastNickName("游客");
			}
		} else {
			postForm.setUserName(userForm.getUserName());
			postForm.setNickName(userForm.getNickName());
			postForm.setLastUserName(userForm.getUserName());
			postForm.setLastNickName(userForm.getNickName());
		}
		postForm.setIp(request.getRemoteAddr());
		postBo.insertReply(postForm);
		postBo.updateLastReplyTime(postForm);
		String path = mapping.findForward("insertReplySuccess").getPath()
				+ "&id=" + postForm.getParentId() + "&boardId="
				+ postForm.getBoardId() + "&boardParentId="
				+ postForm.getBoardParentId();
		return new ActionForward(path, true);
	}

	public void setPostBo(PostBo postBo) {
		this.postBo = postBo;
	}

	public void setBoardBo(BoardBo boardBo) {
		this.boardBo = boardBo;
	}

	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}

}
